//
//  CZWelcomeController.swift
//  swift微博01
//
//  Created by 张文文 on 15/12/17.
//  Copyright © 2015年 张文文. All rights reserved.
//

import UIKit
//1.懒加载空间  2. 添加到界面  3.设置控件约束
class CZWelcomeController: UIViewController {

        //头像底部约束 在初始化的时候确定不了  在prepareUI中才能确定
    private var iconViewBottonCon : NSLayoutConstraint?

    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
    }
    
    
    //view看到的时候做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //头像做动画 修改头像底部约束的值 往上是负数！
        iconViewBottonCon?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        //usingSpringWithDamping 弹簧的效果，越小效果越明显
        //initialSpringVelocity  初始的速度
//        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0)), animations: () -> Void , completion:
//        )
        UIView.animateWithDuration(5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
        
              //动画
            //闭包里面需要加self
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //将lable渐变出来
                self.welcomeLable.alpha = 0
                UIView.animateWithDuration(4.0, animations: { () -> Void in
                    self.welcomeLable.alpha = 1
                    self.welcomeLable.hidden = false
                    }, completion: { (_) -> Void in
                        //_表示展位， 这个参数不关心
                        print("动画完成")
                })
        
        }
    }
    
    
    //MARK 准备ui
    func prepareUI(){
        view.addSubview(backView)
        view.addSubview(iconView)
        view.addSubview(welcomeLable)
    
        backView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLable.translatesAutoresizingMaskIntoConstraints = false
        
        //设置背景图片的约束
        view.addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: backView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //设置头像图片约束
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconViewBottonCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottonCon!)
        //设置lable的约束
        view.addConstraint(NSLayoutConstraint(item: welcomeLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        view.addConstraint(NSLayoutConstraint(item: welcomeLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    }

    //懒加载背景图片
    private lazy var backView : UIImageView = {
    
        let backimage = UIImageView(image: UIImage(named: "ad_background"))
        return backimage
    }()
    //懒加载头像图片
    private lazy var iconView : UIImageView = {
    
        let iconImage = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置头像的圆形切角半径
        iconImage.layer.cornerRadius = 42.5
        iconImage.layer.masksToBounds = true
        
        return iconImage
    }()
    //懒加载lable
    private lazy var welcomeLable : UILabel = {
    
        let lable = UILabel()
        lable.text = "欢迎回来"
        //设置字体大小
        lable.font = UIFont.systemFontOfSize(14)
        //隐藏label
        lable.hidden = true
        return lable
    }()
}
